package com.wyix.controller;

import com.wyix.model.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/book")
public class BookController {

    // 模拟数据库数据
    private static final java.util.List<Book> books = new java.util.ArrayList<>();

    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("books", books);
        return "list"; // 对应WEB-INF/views/list.jsp
    }

    @GetMapping("/add")
    public String toAdd() {
        return "add"; // 对应WEB-INF/views/add.jsp
    }

    @PostMapping("/add")
    public String add(Book book) {
        book.setId((long) (books.size() + 1));
        books.add(book);
        return "redirect:/book/list"; // 重定向到列表页
    }
}
